package com.example.dormitory_system.controller;

import com.example.dormitory_system.aspect.NoCheck;
import com.example.dormitory_system.domain.R;
import com.example.dormitory_system.domain.TableDataInfo;
import com.example.dormitory_system.domain.dto.LoginDTO;
import com.example.dormitory_system.domain.dto.student.*;

import com.example.dormitory_system.domain.dto.tsysuser.ResetPasswordDTO;
import com.example.dormitory_system.domain.dto.tsysuser.StudentResetPasswordDTO;
import com.example.dormitory_system.domain.enetiy.Maintenance;
import com.example.dormitory_system.domain.vo.student.AnnouncementDetailVO;
import com.example.dormitory_system.domain.vo.student.ChangeRoomVO;
import com.example.dormitory_system.domain.vo.student.StuLoginVO;
import com.example.dormitory_system.domain.vo.student.StudentInfoVO;
import com.example.dormitory_system.domain.vo.tsysuser.AnnouncementListVO;
import com.example.dormitory_system.domain.vo.tsysuser.TeacherVO;
import com.example.dormitory_system.domain.vo.user.LoginVO;
import com.example.dormitory_system.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/stu")
public class StudentController extends BaseController {
    @Autowired
    private HolidayStayService holidayStayService;
    @Autowired
    private StudentService studentService;
    @Autowired
    private RoomService roomService;
    @Autowired
    private MaintenanceService maintenanceService;
    @Autowired
    private AnnouncementService announcementService;
    @Autowired
    private UserService userService;

    /**
     * 学生登入
     * @param loginDTO
     * @return
     */
    @PostMapping("/login")
    @NoCheck
    public R<StuLoginVO> login(@RequestBody LoginDTO loginDTO){
        return R.ok(studentService.login(loginDTO));
    }

    @GetMapping("/info")
    public R<StudentInfoVO> info(Long studentId){
        return R.ok(studentService.info(studentId));
    }

    @PutMapping("/info/update")
    public R<Void> updateInfo(@RequestBody StudentInfoUpdateDTO studentInfoUpdateDTO){
        return toR(studentService.updateInfo(studentInfoUpdateDTO));
    }


    /**
     * 学生的留校申请
     * @param holidayStayAddDTO
     * @return
     */
    @PostMapping("/stay/add")
    public R<Void> addHolidayStay(@RequestBody HolidayStayAddDTO holidayStayAddDTO){
        return toR(holidayStayService.addHolidayStay(holidayStayAddDTO));
    }

    /**
     * 学生的留校记录列表
     * @param holidayStayListDTO
     * @return
     */
    @PostMapping("/stay/list")
    public TableDataInfo listHolidayStay(@RequestBody HolidayStayListDTO holidayStayListDTO){
        return getDataTable(holidayStayService.listHolidayStay(holidayStayListDTO));
    }


    /**
     *删除留校记录
     * @param studentId  学生id
     * @param holidayStayId 留校记录id
     * @return
     */
    @PutMapping("/stay/delete")
    public R<Void> deleteHolidayStay(Long studentId,Long holidayStayId){
        return toR(holidayStayService.deleteHolidayStay(studentId,holidayStayId));
    }

    /**
     * 换宿舍
     * @param changeRoomDTO
     * @return
     */
    @PostMapping("/room/update")
    public R<Void> updateRoom(@RequestBody ChangeRoomDTO changeRoomDTO){
        return toR(roomService.updateRoom(changeRoomDTO));
    }

    /**
     *查看换宿舍记录
     * @param studentId 学生
     * @param pageNum  分页
     * @param pageSize
     * @return
     */
    @GetMapping("/room/list")
    public TableDataInfo listRoom(Long studentId,@RequestParam (defaultValue = "1") Integer pageNum,@RequestParam (defaultValue = "10") Integer pageSize){
        //调用roomService的listRoom方法，传入学生id、分页和每页大小，返回TableDataInfo对象
        return getDataTable(roomService.listRoom(studentId,pageNum,pageSize));
    }

    /**
     *查看换宿舍结果
     * @param studentId 学生id
     * @param changeRoomId 换宿舍记录id
     * @return
     */
   /* @GetMapping("/room/result")
    public R<ChangeRoomVO> getRoomResult(Long studentId,Long changeRoomId){
        return R.ok(roomService.getRoomResult(studentId,changeRoomId));
    }
*/
    /**
     * 查看维修记录
     * @param studentId
     * @param pageNum
     * @param pageSize
     * @return
     */
    @GetMapping("/maintain/list")
    public TableDataInfo listMaintain(Long studentId,@RequestParam (defaultValue = "1") Integer pageNum,@RequestParam (defaultValue = "10") Integer pageSize){   //未分页
        return getDataTable(maintenanceService.listMaintain(studentId,pageNum,pageSize));
    }

    /**
     * 添加维修记录
     * @param maintenanceAddDTO
     * @return
     */
    @PostMapping ("/maintain/add")
    public R<Void> addMaintain(@RequestBody MaintenanceAddDTO maintenanceAddDTO){
        return toR(maintenanceService.addMaintain(maintenanceAddDTO));
    }


    /**
     * 学生查看公告列表 支持模糊匹配
     * @param pageNum
     * @param pageSize
     * @param title 模糊匹配 可以不传
     * @return
     */
    @NoCheck
    @GetMapping("/announcement/list")
    public TableDataInfo listAnnouncement(@RequestParam (defaultValue = "1") Integer pageNum,@RequestParam (defaultValue = "10") Integer pageSize,String title){
        return getDataTable(announcementService.listAnnouncement(pageNum,pageSize,title));
    }

    /**
     * 查看公告详情
     * @param announcementId 公告id
     * @return
     */
    @NoCheck
    @GetMapping("/announcement/detail")
    public R<AnnouncementDetailVO> getAnnouncementDetail(Long announcementId){
        return R.ok(announcementService.getAnnouncementDetail(announcementId));
    }


    /**
     * 学生查询老师
     * @param name
     * @return
     */
    @GetMapping("/listTeacher")
    public R<TeacherVO> listTeacher(String name){
        return R.ok(userService.searchTeacher(name));
    }

    /**
     * 学生查看审核老师列表 模糊匹配
     * @param name  模糊匹配老师名字 可以不传
     * @return
     */
    @GetMapping("/user/list")
    public TableDataInfo listUser(String name) {
        return getDataTable(userService.listUser(name));
    }

    /**
     * 重置密码
     * @param resetPasswordDTO
     * @return
     */
    @PostMapping("/resetPassword")
    public R<Boolean> resetPassword(@RequestBody StudentResetPasswordDTO resetPasswordDTO){
        return R.ok(userService.resetpassoword(resetPasswordDTO.getStudentId(),resetPasswordDTO.getOldPassword(),resetPasswordDTO.getNewPassword()));
    }


}
